//
//  JZHomeJingxuanCell.m
//  NuoMi
//
//  Created by liangyi on 15/12/3.
//  Copyright © 2015年 liangyi. All rights reserved.
//

#import "JZHomeJingxuanCell.h"
@interface JZHomeJingxuanCell()
{
    NSTimeInterval _startTime;
    NSTimeInterval _nowTime;
    

}

@end
@implementation JZHomeJingxuanCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier listArray:(NSMutableArray *)listArray activeTimeArray:(NSMutableArray *)activeTimeArray
{
    self  = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        UILabel *labelContent =  [[UILabel alloc]initWithFrame:CGRectMake(160, 10, 70, 30)];
        [self.contentView addSubview:labelContent];
        labelContent.font = [UIFont systemFontOfSize:10];
        labelContent.text = @"距结束";
        labelContent.textColor = [UIColor grayColor];
        
        UILabel *labelTitle =  [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 70, 30)];
        [self.contentView addSubview:labelTitle];
        labelTitle.font = [UIFont systemFontOfSize:15];
        labelTitle.text = @"精选抢购";
        labelTitle.textColor = [UIColor blackColor];
        _listArray = listArray;
        
        self.hourLabel = [[UILabel alloc]initWithFrame:CGRectMake(200, 10, 120, 30)];
        self.minLabel = [[UILabel alloc]initWithFrame:CGRectMake(230, 10, 30, 30)];
        self.secLabel = [[UILabel alloc]initWithFrame:CGRectMake(260, 10, 30, 30)];
        [self.contentView addSubview:_hourLabel];
        [self.contentView addSubview:_minLabel];
        [self.contentView addSubview:_secLabel];

        if (listArray.count == 3) {
            for (int i = 0; i < 3; i++) {
                
               
                
                _shopNameLabel =[[UILabel alloc]initWithFrame:CGRectMake(100 * i + 30, 110, 130, 30)];
                [self.contentView addSubview:_shopNameLabel];
                _shopNameLabel.font = [UIFont systemFontOfSize:11];
                _newPriceLabel = [[UILabel alloc]initWithFrame:CGRectMake(100 * i + 20, 140, 80, 30)];
                _newPriceLabel.textAlignment = NSTextAlignmentLeft;
                  [self.contentView addSubview:_newPriceLabel];
                _newPriceLabel.font = [UIFont systemFontOfSize:14];
                _newPriceLabel.textColor = [UIColor redColor];
                _oldPriceLabel =[[UILabel alloc]initWithFrame:CGRectMake(100 * i + 65, 140, 80, 30)];
                _oldPriceLabel.textAlignment = NSTextAlignmentLeft;
                  [self.contentView addSubview:_oldPriceLabel];
                _oldPriceLabel.font = [UIFont systemFontOfSize:11];
                                _oldPriceLabel.textColor = [UIColor redColor];
                _imageview = [[UIImageView alloc]initWithFrame:CGRectMake(100 * i + 18, 40, 70, 70)];
                _imageview.tag = 10 + i;
                
                [self.contentView addSubview:_imageview];
                _imageview.userInteractionEnabled = YES;
                UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
                [_imageview addGestureRecognizer:tap];
                _shopNameLabel.text = [listArray[i] objectForKey:@"brand"];
                

                NSLog(@"%@", _shopNameLabel.text);
                
                _newPriceLabel.text = [NSString stringWithFormat:@"￥%ld",[[listArray[i] objectForKey:@"current_price"] integerValue]/100];
                
                NSString *oldStr = [NSString stringWithFormat:@"%ld",[[listArray[i] objectForKey:@"market_price"] integerValue]/100];
                //中划线
                NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
                NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:oldStr attributes:attribtDic];
                _oldPriceLabel.attributedText = attribtStr;
                //
                
                NSString *imgStr = [listArray[i] objectForKey:@"na_logo"];
                NSRange range = [imgStr rangeOfString:@"src="];
                if (range.location != NSNotFound) {
                    NSString *subStr = [imgStr substringFromIndex:range.location+range.length];
                    subStr = [subStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    [_imageview sd_setImageWithURL:[NSURL URLWithString:subStr] placeholderImage:[UIImage imageNamed:@"ugc_photo"]];
                    
                    
                }
            }
        }else{
            
        }
        
        _activeTimeArray = activeTimeArray;
        NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
        _nowTime=[dat timeIntervalSince1970];
        NSString *timeString = [NSString stringWithFormat:@"%.0f", _nowTime];
        NSLog(@"=== %@",timeString);
        for (int i = 0; i < activeTimeArray.count; i++) {
            NSInteger endtime = [[activeTimeArray[i] objectForKey:@"starttime"] integerValue];
            if (_nowTime < endtime) {
                _startTime = endtime;
                [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateUI) userInfo:nil repeats:YES];
                break;
            }
        }


    }
    return self;
    
}
//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
//    [super setSelected:selected animated:animated];
//    
//    // Configure the view for the selected state
//}

-(void)OnTapBtnView:(UITapGestureRecognizer *)sender{
    NSLog(@"tag:%ld",sender.view.tag);
    [self.delegate homeJingxuanDidSelectedAtIndex:sender.view.tag];
    
    
    
    
}

-(void)updateUI{
    [self performSelectorOnMainThread:@selector(setTimeLabel) withObject:nil waitUntilDone:NO];
    //    [self setTimeLabel];
}

-(void)setTimeLabel{
    NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
    _nowTime=[dat timeIntervalSince1970];
    if (_startTime <= _nowTime) {
        return;
    }
  
    
       NSInteger subTime = _startTime - _nowTime;
    NSInteger hour = subTime/3600;
    NSInteger min = (subTime%3600)/60;
    NSInteger sec = (subTime%3600)%60;
    
    self.hourLabel.text = [NSString stringWithFormat:@"%02ld:%02ld:%02ld",hour, min, sec];
//    self.minLabel.text = [NSString stringWithFormat:@"%02ld:",min];
//    self.secLabel.text = [NSString stringWithFormat:@"%02ld",sec];
    
}




@end
